﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using CGSB.AppCode;
using CDCW.API;
using CDCW.Data;

namespace CGSB.View
{
    public partial class MangerBranch : BasePage
    {
        public string strAction
        {
            get
            {
                return Request.QueryString["Action"].ToString();
            }
        }

        public string strPBanchCode
        {
            get
            {
                return Request.QueryString["PID"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialData();
            }
        }

        protected void btAddBranch_Click(object sender, EventArgs e)
        {
            if (ChenckData())
            {
                int pid = int.Parse(strPBanchCode);
                if (strAction == "add")
                {
                    JH_Auth_Branch NewBranch = new JH_Auth_Branch();
                    NewBranch.DeptName = tbBranchName.Text;
                    NewBranch.DeptRoot = int.Parse(strPBanchCode);
                    NewBranch.DeptShort = int.Parse(conXH.Text);
                    NewBranch.DeptDesc = "";
                    NewBranch.BranchLevel = 0;
                    
                    JH_Auth_Branch BranchUP = new JH_Auth_BranchB().GetEntities(d => d.DeptCode == pid).SingleOrDefault();
                    if (BranchUP != null)
                    {
                        int number = 0;
                        List<JH_Auth_Branch> branchList = new JH_Auth_BranchB().GetEntities(" DeptRoot='" + strPBanchCode + "'").OrderBy(d => d.DeptCode).ToList<JH_Auth_Branch>();
                        if (branchList.Count > 0)
                        {
                            string code = branchList.LastOrDefault().Remark1;
                            int.TryParse(code, out number);
                            NewBranch.Remark1 = (number + 1).ToString();
                        }
                        else
                        {
                            NewBranch.Remark1 = (strPBanchCode + "1").ToString();
                        }
                    }
                    else
                    {
                        int number = 0;
                        int.TryParse(new JH_Auth_BranchB().GetEntities(d => d.DeptRoot == BranchUP.DeptRoot).OrderBy(d => d.DeptCode).LastOrDefault().Remark1, out number);
                        NewBranch.Remark1 = (number + 1).ToString();
                    }
                    new JH_Auth_BranchB().Insert(NewBranch);
                    try
                    {

                        AlertMSG("3", "添加部门成功！");

                    }
                    catch (Exception)
                    {
                        new JH_Auth_BranchB().Delete(NewBranch);
                        AlertMSG("3", "添加部门失败！");
                    }
                }
                else
                {
                    JH_Auth_Branch BranchUP = new JH_Auth_BranchB().GetEntities(d => d.DeptCode == pid).SingleOrDefault();
                    BranchUP.DeptName = tbBranchName.Text;
                    BranchUP.DeptRoot = int.Parse(ddlBranch.SelectedValue);
                    BranchUP.DeptShort = int.Parse(conXH.Text.Trim() == "" ? "0" : conXH.Text);


                    try
                    {
                        new JH_Auth_BranchB().Update(BranchUP);
                        AlertMSG("3", "修改部门成功！");

                    }
                    catch (Exception)
                    {
                        AlertMSG("3", "修改部门失败！");
                    }
                }
            }
            else
            {
                return;
            }
        }

        /// <summary>
        /// 服务器端验证
        /// </summary>
        /// <returns></returns>
        public bool ChenckData()
        {
            bool boolcheck = true;
            if (tbBranchName.Text.ToString() == "")
            {
                AlertMSG("1", "部门名称不能为空！");
                boolcheck = false;
            }
            return boolcheck;
        }

        public void InitialData()
        {
            new CommonHelp.BindData().BindBranch(ddlBranch, "Y");
            ddlBranch.Items.Insert(0, new System.Web.UI.WebControls.ListItem("请选择", "-1"));

            int pid = int.Parse(strPBanchCode);
            if (strAction != "add")
            {
                JH_Auth_Branch Branch = new JH_Auth_BranchB().GetEntities(d => d.DeptCode == pid).SingleOrDefault();
                tbBranchName.Text = Branch.DeptName;
                ddlBranch.SelectedValue = Branch.DeptRoot.ToString();
                conXH.Text = Branch.DeptShort.ToString();
            }
            else
            {
                ddlBranch.Enabled = false;
                ddlBranch.SelectedValue = strPBanchCode;

            }
        }
    }
}